package me.samlss.lighter_demo.slice;

import me.samlss.lighter.Lighter;
import me.samlss.lighter.parameter.Direction;
import me.samlss.lighter.parameter.LighterParameter;
import me.samlss.lighter.parameter.MarginOffset;
import me.samlss.lighter.shape.OvalShape;
import me.samlss.lighter.shape.RectShape;
import me.samlss.lighter_demo.LighterHelper;
import me.samlss.lighter_demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class ViewPagerAbilitySlice extends AbilitySlice {
    DependentLayout view_pager_root;
    Text title_tv;
    Image back_iv;
    PageSlider page_slider;
    private List<Component> mViewList = new ArrayList<>();

    private Lighter mLighter1;
    private Lighter mLighter2;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_view_pager);
        initView();
        initViewPager();
    }

    private void initView() {
        view_pager_root = (DependentLayout) findComponentById(ResourceTable.Id_view_pager_root);
        title_tv = (Text) findComponentById(ResourceTable.Id_title_tv);
        title_tv.setText("ViewPager");
        back_iv = (Image) findComponentById(ResourceTable.Id_back_iv);
        back_iv.setClickedListener(component -> getAbility().terminateAbility());
        page_slider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
    }

    private void initViewPager() {
        Component component1 = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_viewpager_1, view_pager_root, false);
        Component component2 = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_viewpager_2, view_pager_root, false);

        mViewList.add(component1);
        mViewList.add(component2);

        page_slider.setProvider(new Adapter());
        page_slider.addPageChangedListener(mPageChangeListener);
        showPage1Guide();
    }


    @Override
    protected void onStop() {
        super.onStop();
        mViewList.clear();
        page_slider.removePageChangedListener(mPageChangeListener);
    }

    private void showPage1Guide() {
        if (mLighter1 != null && mLighter1.isShowing()) {
            return;
        }
        mLighter1 = Lighter.with((ComponentContainer) mViewList.get(0))
                .setBackgroundColor(0x000000, 0.5f)
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_1)
                        .setTipLayoutId(ResourceTable.Layout_layout_tip_1)
                        .setLighterShape(new OvalShape())
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .setTipViewRelativeDirection(Direction.RIGHT)
                        .setTipViewRelativeOffset(new MarginOffset(30, 0, 80, 0))
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_2)
                        .setTipLayoutId(ResourceTable.Layout_layout_tip_7)
                        .setLighterShape(new OvalShape())
                        .setTipViewRelativeDirection(Direction.LEFT)
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .setTipViewRelativeOffset(new MarginOffset(50, 0, 100, 0))
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_3)
                        .setTipLayoutId(ResourceTable.Layout_layout_tip_2)
                        .setLighterShape(new OvalShape())
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .setTipViewRelativeOffset(new MarginOffset(-400, 0, 0, 30))
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_4)
                        .setTipLayoutId(ResourceTable.Layout_layout_tip_3)
                        .setLighterShape(new OvalShape())
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setTipViewRelativeOffset(new MarginOffset(80, 0, 0, 20))
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .build())
                .addHighlight(new LighterParameter.Builder()
                        .setHighlightedViewId(ResourceTable.Id_vp_btn_5)
                        .setTipLayoutId(ResourceTable.Layout_layout_tip_4)
                        .setLighterShape(new OvalShape())
                        .setTipViewRelativeDirection(Direction.TOP)
                        .setTipViewRelativeOffset(new MarginOffset(0, 100, 0, 20))
                        .setTipViewDisplayAnimation(LighterHelper.getScaleAnimation())
                        .build());
        mLighter1.show();
    }

    private void showPage2Guide() {
        if (mLighter2 != null && mLighter2.isShowing()){
            return;
        }

        mLighter2 = Lighter.with((ComponentContainer) mViewList.get(1))
                .setBackgroundColor(0x000000, 0.5f)
                .addHighlight(
                        //Show three at a time
                        new LighterParameter.Builder()
                                .setHighlightedViewId(ResourceTable.Id_vp_btn_1)
                                .setTipLayoutId(ResourceTable.Layout_layout_tip_1)
                                .setLighterShape(new RectShape())
                                .setTipViewRelativeDirection(Direction.BOTTOM)
                                .setTipViewRelativeOffset(new MarginOffset(200, 0, 30, 0))
                                .build(),

                        new LighterParameter.Builder()
                                .setHighlightedViewId(ResourceTable.Id_vp_btn_2)
                                .setTipLayoutId(ResourceTable.Layout_layout_tip_2)
                                .setLighterShape(new RectShape())
                                .setTipViewRelativeDirection(Direction.TOP)
                                .setTipViewRelativeOffset(new MarginOffset(-400, 0, 0, 30))
                                .build(),

                        new LighterParameter.Builder()
                                .setHighlightedViewId(ResourceTable.Id_vp_btn_3)
                                .setTipLayoutId(ResourceTable.Layout_layout_tip_3)
                                .setLighterShape(new RectShape())
                                .setTipViewRelativeDirection(Direction.TOP)
                                .setTipViewRelativeOffset(new MarginOffset(100, 0, 0, 30))
                                .build());
        mLighter2.show();
    }

    private PageSlider.PageChangedListener mPageChangeListener = new PageSlider.PageChangedListener() {

        @Override
        public void onPageSliding(int i, float v, int i1) {

        }

        @Override
        public void onPageSlideStateChanged(int i) {

        }

        @Override
        public void onPageChosen(int i) {
            if (i == 0) {
                showPage1Guide();
            } else {
                showPage2Guide();
            }
        }
    };


    class Adapter extends PageSliderProvider {

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            Component component = mViewList.get(i);
            componentContainer.addComponent(component);
            return component;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
            componentContainer.removeComponent(mViewList.get(i));
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return component == o;
        }
    }
}
